
#include <stdio.h>


#include <cv.h>
#include <highgui.h>
#include <ml.h>
#include "resourceTracker.h"

/*
 *  GestureClassifier_scrap.cpp
 *  
 *
 *  Created by Rasmus Kyng on 11/01/2011.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */


//class GestureClassifier {
//public:
//	train( CvDTreeTrainData*
//
//}

using namespace cv;

int main(int argc, const char* argv[]) {
	
	const char* videoNameString;
	
	if (argc == 1) {
		videoNameString = "locVid1c";
	} else {
		videoNameString = argv[1];
	}
	
	//Trying convexity defect first
	//CvBoost* boostClassifier = new CvBoost();
	
	ResourceTracker* rt = new ResourceTracker( "../config/runtimeSettings/cfg.xml" );
	rt->loadCategory("TestData");
	
	cv::Ptr< cv::VideoCapture > capPtr;
	cv::Ptr< VideoPointTagSequence > seqPtr;
	

	
	capPtr = rt->getFileVideoCapture( videoNameString );
	seqPtr = rt->getPointTags( videoNameString );
	
	int handRectSideLength = 200;
	
	Rect handRect = Rect( 0, 0, handRectSideLength, handRectSideLength);
	
	int tagCount = seqPtr->tagCount;
	
	// -- visualisation: set up -- //
	
	char* mainWindowName = "classifier scrap";
	char* handFrameWindowName = "classifier scrap -- hand rect";
	char* edgeWindowName = "classifier scrap -- hand rect, edges";
	char* contourWindowName = "classifier scrap -- hand rect, contours";
	
	namedWindow( mainWindowName, 1 );
	namedWindow( handFrameWindowName, 1 );
	namedWindow( edgeWindowName, 1 );
	namedWindow( contourWindowName, 1 );
	
	//Note: using OpenCV/C function as this has not been implemented in OpenCV/C++
	cvMoveWindow( mainWindowName, 0, 100 );
	cvMoveWindow( handFrameWindowName, 700, 100 );
	cvMoveWindow( edgeWindowName, 700, 300 );
	cvMoveWindow( contourWindowName, 700, 500 );
	
	Mat frame;
	
	for( int frameCount = 0; frameCount<tagCount; frameCount++) {
		
		(*capPtr) >> frame;
		
		// -- tracking frame -- //
		Point groundTruth = seqPtr->getNextPointTag();
		handRect = Rect( groundTruth.x - handRect.width/2, groundTruth.y - handRect.height/2, handRect.width, handRect.height );
		
		//forcing rectangle to stay inside frame
		if ( handRect.x < 0 ) handRect.x = 0;
		if ( frame.cols < handRect.x + handRect.width ) handRect.x = frame.cols - handRect.width;
		if ( handRect.y < 0 ) handRect.y = 0;
		if ( frame.rows < handRect.y + handRect.height ) handRect.y = frame.rows - handRect.height;
		
		
		// -- segmentation on hand ROI -- //
		
		Mat handImg(frame, handRect);
		
		Mat handGreyImg;
		
		// convert image to grayscale
		cvtColor( handImg, handGreyImg, CV_BGR2GRAY );
		
		Mat handEdgeImg;
		
		Canny( handGreyImg, handEdgeImg, 5, 100 );
		
		vector<vector<Point> > contours;
		vector<Vec4i> hierarchy;
		
		findContours( handEdgeImg, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );
		
		Mat contoursImg = Mat::zeros(handEdgeImg.size(), CV_8UC3);
		
		//
		
		
			
		// -- visualisation: display -- //
		
		{
			rectangle( frame, Point(handRect.x, handRect.y),
					  Point(handRect.x + handRect.width, handRect.y + handRect.height),
					  Scalar::all(250));
			
			circle( frame, groundTruth, 1, Scalar(0, 255, 0), 2 ); //radius 1, thickness 2, colour green
			//NOTE: BGR colour!
			
			
			if( contours.size() > 0 )
			{
				// iterate through all the top-level contours,
				// draw each connected component with its own random color
				int idx = 0;
				for( ; idx >= 0; idx = hierarchy[idx][0] )
				{
					Scalar color( (rand()&255)+10, (rand()&255)+10, (rand()&255)+10 );
					drawContours( contoursImg, contours, idx, color, CV_FILLED, 8, hierarchy );
				}
			}
			
			
			imshow( mainWindowName, frame );
			imshow( handFrameWindowName, handImg );
			imshow( edgeWindowName, handEdgeImg );
			imshow( contourWindowName, contoursImg );
			

			
			
		}
		
		// -- get next frame -- //
		
		waitKey(frameCount*3+1);
	}
	

	
	delete rt;
	
	//delete boostClassifier;

	return 0;

}